package com.example.administrator.capital.mine.investment;


import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.CommentAdapter;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.login.LoginActivity;
import com.example.administrator.capital.model.TestBean;
import com.example.administrator.capital.model.bean.InvestBean;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import layout.LoadingLayout;
import recycle.BaseViewHolder;

/**
 * A simple {@link Fragment} subclass.
 * 出借
 */
public class LoanInvestmentFragment extends BaseBackFragment {


    @BindView(R.id.recycle_view)
    RecyclerView mRecycleView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.loading)
    LoadingLayout mLoading;
    Unbinder unbinder;
    private CommentAdapter<InvestBean.DataBean.ListBean> mAdapter;
    private List<TestBean> mTestBeans = new ArrayList<>();
    private int mPage;

    public LoanInvestmentFragment() {
        // Required empty public constructor
    }

    public static LoanInvestmentFragment newInstance() {

        Bundle args = new Bundle();
        LoanInvestmentFragment fragment = new LoanInvestmentFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_loan_investment, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }
    @Override
    protected void initView(View view) {
        super.initView(view);
        mSwipeLayout.setColorSchemeResources(R.color.font_blue);
        mAdapter = new CommentAdapter<InvestBean.DataBean.ListBean>(R.layout.investment_item_list,null) {
            @Override
            public void setViewData(BaseViewHolder holder, InvestBean.DataBean.ListBean item, int position) {
                holder .setText(R.id.name,item.getTitle())
                        .setText(R.id.shouyi,item.getRate()+"")
                        .setText(R.id.time,item.getRate()+"%")
                        .setText(R.id.support,Integer.valueOf(item.getAmount())/100+"");
            }

            @Override
            public void setEvent(BaseViewHolder holder, InvestBean.DataBean.ListBean item, int position) {

            }
        };
        mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration div = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        div.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider));
        mRecycleView.addItemDecoration(div);
        mRecycleView.setAdapter(mAdapter);
        mAdapter.setEnableLoadMore(true);
        minePresenter=new MinePresenter(null);
    }
    @Override
    protected void setListener() {
        super.setListener();
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(1,true);
            }
        });
    }

    @Override
    protected void setData() {
        super.setData();
        getData(1,true);

    }
    private MinePresenter minePresenter;
    private void getData(final int page, final boolean isRefresh) {
        minePresenter.investMent(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                if (var.equals("请登录")) {
                    mBaseActivity.showActivity(LoginActivity.class,null);
                } else {
                    mLoading.setEmptyImage(R.drawable.empty);
                    mLoading.showEmpty();
                }
                mSwipeLayout.setRefreshing(false);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
                mSwipeLayout.setRefreshing(false);
                mLoading.setErrorImage(R.drawable.error);
                mLoading.showError();
            }
            @Override
            public void onSuccess(Response<String> data) {
                InvestBean bean = JsonUtils.parse(data.body(), InvestBean.class);
                mAdapter.setEnableLoadMore(true);
                if (isRefresh) {
                    mAdapter.setNewData(bean.getData().getList());
                    mPage = 1;
                } else {
                    if (bean.getData().getList().size() > 0) {
                        mAdapter.addData(bean.getData().getList());
                    }
                }
                if (bean.getData().getList().size() < 5) {
                    mAdapter.setEnableLoadMore(false);
                    //第一页如果不够一页就不显示没有更多数据布局
                } else {
                    mAdapter.loadMoreComplete();
                }
                mSwipeLayout.setRefreshing(false);
                mLoading.showContent();
            }
            @Override
            public void onFail(Response<String> error) {

            }
        }, PreferenceManager.instance().getUseId(),mPage,"2");

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
